#ifndef X_THREAD_H
#define X_THREAD_H

#include <process.h>
#include <stdlib.h>
#include <stdexcept>
using std::runtime_error;

using namespace std;

template <class T>

class XepdtThread
{
        T* worker_;
        typedef void* (T::*PMF)(void*) ;
        PMF pmf_;
        void * arg_;
		
        static UINT WINAPI run(void* me) 
		{
                T* t = ((XepdtThread*)me)->worker_;
                PMF pmf = ((XepdtThread*)me)->pmf_;
                void* arg = ((XepdtThread*)me)->arg_;
                (t->*pmf)(arg);
				return 0;
        }

public:
        XepdtThread(T* w, PMF pmf, void* arg = NULL) :
                    worker_(w), pmf_(pmf), arg_(arg), handle_(NULL)
		{
		}

        ~XepdtThread() 
		{
			if(handle_)
			{
				CloseHandle(handle_);
			}
        }

        void start()
		{
            handle_ = (HANDLE)_beginthreadex( 0, 0, XepdtThread<T>::run, this, 0, 0);
        }

		HANDLE handle_;
};

#endif //X_THREAD_H

